package com.gxuwz.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.gxuwz.entity.User;
import com.gxuwz.service.UserService;

public class UserServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private UserService userService = null;
	private User user = null;
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		// 用户标记用户在页面执行了哪个操作
		String mark = request.getParameter("mark");
		if("login".equals(mark)){
			login(request,response);
		}else if("showAll".equals(mark)){ 
			showAll(request,response);
		}else if("insert".equals(mark)){ 
			insert(request,response);
		}else if("update".equals(mark)){ 
			update(request,response);
		}else if("delete".equals(mark)){ 
			delete(request,response);
		}else if("openUpdateView".equals(mark)){ 
			openUpdateView(request,response);
		}else if("findByUsername".equals(mark)){ 
			findByUsername(request,response);
		}else{
			
		}
		
	}

	private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		// 使用之前需要 new 一个对象出来
		userService = new UserService();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		user = userService.findByUsernameAndPassword(username,password);	
		
		if(null != user){
	        // 使用request对象的getSession()获取session，如果session不存在则创建一个
	        HttpSession session = request.getSession();
	        // 将用户信息存储到session中
	        session.setAttribute("loginUser", user);
     	    // 获取到用户的角色，根据用户的角色跳转到不同的页面	
	        System.out.println(user.getRoleId());
		 if (user.getRoleId()==1) {// 超级管理员
			request.getRequestDispatcher("views/common/main.jsp").forward(
					request, response);
		} else if (user.getRoleId()==2) {// 采购员
			request.getRequestDispatcher("views/common/main_2.jsp").forward(
					request, response);
		} else if (user.getRoleId()==3) {// 仓库管理员
			request.getRequestDispatcher("views/common/main_3.jsp").forward(
					request, response);
		} else if (user.getRoleId()==4) {// 销售员
			request.getRequestDispatcher("views/common/main_4.jsp").forward(
					request, response);
		}
		
		}else{
			request.setAttribute("msg" , "用户名或密码错误");
			request.getRequestDispatcher("views/login.jsp").forward(request, response);	
		}
}
	
	
	public void showAll(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
		userService = new UserService();
        List<User> list = userService.findAll();
        request.setAttribute("list", list);
        // 转发
        request.getRequestDispatcher("views/user/user_list.jsp").forward(request, response); 
    }
	
	private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		userService = new UserService();
		userService.delete(id);
		showAll(request, response);
	}

	private void update(HttpServletRequest request, HttpServletResponse response) {		
		User user = getWebUser(request);		
		userService = new UserService();
		userService.update(user);
	}

	private void insert(HttpServletRequest request, HttpServletResponse response) {	
		User user = getWebUser(request);	
		userService = new UserService();
		userService.insert(user);		
	}
	
	private void openUpdateView(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		userService = new UserService();
		// 打开更新用户的页面需要传递用户的初始数据
		user = userService.findById(id);
		request.setAttribute("user", user);
		request.getRequestDispatcher("views/user/user_update.jsp").forward(request, response); 
	}
	
	private void findByUsername(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
        List<User> list = userService.findByUsername(username);
        request.setAttribute("list", list);
        // 转发
        request.getRequestDispatcher("views/user/user_list.jsp").forward(request, response); 
		
	}
	
	/**
	 * 
	 * <p>Description: 封装网页传来的用户数据，供插入、更新使用</p>
	 * <p>Company: 梧州学院</p> 
	 * @author 刘玉霜
	 * @date 2017年12月11日 下午2:13:12
	 * @return
	 */
	public User getWebUser(HttpServletRequest request){
		String userId = request.getParameter("userId");
		String age = request.getParameter("age");
		String sex = request.getParameter("sex");
		String name = request.getParameter("name");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String roleId = request.getParameter("role");
		
		User user = new User();
		if(null != userId && !"".equals(userId)){
			user.setUserId(Integer.valueOf(userId));
		}
		user.setName(name);
		user.setAge(Integer.valueOf(age));
		user.setSex(Integer.valueOf(sex));
		user.setUsername(username);
		user.setPassword(password);
		user.setRoleId(Integer.valueOf(roleId));
		return user;
	}
		
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}

}
